package org.cloud.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.cloud.Enum.CommentLevel;
import org.cloud.mapper.*;
import org.cloud.pojo.*;
import org.cloud.service.ItemService;
import org.cloud.utils.PagedGridResult;
import org.cloud.vo.ItemCommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemsMapper itemsMapper;

    @Autowired
    private ItemsImgMapper itemsImgMapper;

    @Autowired
    private ItemsSpecMapper itemsSpecMapper;

    @Autowired
    private ItemsParamMapper itemsParamMapper;

    @Autowired
    private ItemsMapperCustom itemsMapperCustom;

    @Autowired
    private ItemsCommentsMapper itemsCommentsMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Items queryItemById(String id) {
        return itemsMapper.selectByPrimaryKey(id);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ItemsImg> queryItemImgList(String itemId) {
        Example example = new Example(ItemsImg.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId",itemId);

        return itemsImgMapper.selectByExample(example);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ItemsSpec> queryItemSpecList(String itemId) {
        Example example = new Example(ItemsSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId",itemId);

        return itemsSpecMapper.selectByExample(example);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ItemsParam queryItemParamList(String itemId) {
        Example example = new Example(ItemsParam.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId",itemId);

        return itemsParamMapper.selectOneByExample(example);
    }

    @Override
    public CommentLevelCounts queryCommentCounts(String itemId) {
        Integer goodCounts= getCommentCounts(itemId, CommentLevel.GOOD.type);
        Integer badCounts = getCommentCounts(itemId, CommentLevel.BAD.type);
        Integer normalCounts = getCommentCounts(itemId, CommentLevel.NORMAL.type);
        Integer totalCounts = goodCounts + badCounts + normalCounts;
        CommentLevelCounts commentLevelCounts = new CommentLevelCounts();
        commentLevelCounts.setBadCounts(badCounts);
        commentLevelCounts.setGoodCounts(goodCounts);
        commentLevelCounts.setNormalCounts(normalCounts);
        commentLevelCounts.setTotalCounts(totalCounts);
        return commentLevelCounts;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    Integer getCommentCounts(String itemId, Integer level) {
        ItemsComments comments = new ItemsComments();
        comments.setItemId(itemId);
        if (level != null) {
            comments.setCommentLevel(level);
        }
        return itemsCommentsMapper.selectCount(comments);
    }

    @Override
    public PagedGridResult queryPagedComments(String itemId, Integer level
                                                    ,Integer page,Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("itemId",itemId);
        map.put("level",level);

        PageHelper.startPage(page,pageSize);

        List<ItemCommentVo> list = itemsMapperCustom.queryItemComments(map);

        return setPagedGrip(list, page);
    }

    private PagedGridResult setPagedGrip(List<?> list, Integer page) {
        PageInfo<?> pageInfo = new PageInfo<>(list);
        PagedGridResult grid = new PagedGridResult();
        grid.setPage(page);
        grid.setRows(list);
        grid.setTotal((int) pageInfo.getTotal());
        grid.setRecords(pageInfo.getPages());

        return grid;
    }
}
